<template>
  <div class="box" :class="{'show': isShow}">
    <div class="title">回到顶部</div>
    <div class="topBtn" @click="backTop">
      <span class="icon icon-font icon-goTop"></span>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      isShow: false
    }
  },
  methods: {
    backTop () {
      let timer = setInterval(function () {
        let os = document.documentElement || document.body
        // let MainNode = os.getElementsByClassName('main')[0]
        let osTop = os.scrollTop
        let ispeed = Math.floor(osTop / 2)
        // os.scrollTop = osTop - ispeed
        window.scrollTo(0, osTop - ispeed)
        if (osTop < 5) {
          clearInterval(timer)
        }
      }, 30)
    }
  },
  mounted () {
    let os = document.documentElement || document.body
    // let MainNode = os.getElementsByClassName('main')[0]
    window.addEventListener('scroll', (e) => {
      if (os.scrollTop > 100 || document.body.scrollTop > 100) {
        this.isShow = true
      } else {
        this.isShow = false
      }
    }, false)
  }
}
</script>

<style scoped lang="scss">
.box {
  position: fixed;
  bottom: 10px;
  right: 25px;
  width: 70px;
  display: none;
  &.show {
    display: block;
  }
  .title {
    color: $stable;
    text-align: center;
  }
  .topBtn {
    width: 100%;
    height: 50px;
    text-align: center;
    line-height: 50px;
    background-color: #000;
    transition: all 0.5s;
    opacity: 0.3;
    cursor: pointer;
    > span {
      font-size: 25px;
      color: #fff;
    }
    &:hover {
      opacity: 0.5;
    }
  }
}
</style>
